package com.ynet.eq.haiqing.common.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 胡超雄
 * @date 2021/7/16 16:04
 * @desc mybatis Plus 自动填充功能处理器
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object createdAt = this.getFieldValByName("createdAt", metaObject);
        if (createdAt == null) {
            this.setFieldValByName("createdAt", new Date(), metaObject);
        }
        Object createdBy = this.getFieldValByName("createdBy", metaObject);
        if (createdBy == null) {
            this.setFieldValByName("createdBy", this.getModifyUserName(), metaObject);
        }


    }


    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("changedAt", new Date(), metaObject);
        this.setFieldValByName("changedBy", this.getModifyUserName(), metaObject);
    }

    /**
     * 获取操作人
     *
     * @return 当前操作人
     */
    private String getModifyUserName() {
        return "胡超雄";
    }
}
